package com.dog.modules.system.web;

import com.dog.framework.core.model.PageResult;
import com.dog.framework.core.model.ResponseResult;
import com.dog.framework.log.annotation.OperateLog;
import com.dog.framework.log.enums.BusinessType;
import com.dog.modules.system.convert.SysDictDataConvert;
import com.dog.modules.system.domain.SysDictData;
import com.dog.modules.system.api.query.SysDictDataQuery;
import com.dog.modules.system.api.vo.SysDictDataVO;
import com.dog.modules.system.service.SysDictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典数据
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/sys/dict/data")
@Tag(name = "字典数据")
@AllArgsConstructor
public class SysDictDataController {

    private final SysDictDataService sysDictDataService;

    @PostMapping("/page")
    @Operation(summary = "分页")
    public ResponseResult<PageResult<SysDictDataVO>> page(@RequestBody @Valid SysDictDataQuery query) {
        PageResult<SysDictDataVO> page = sysDictDataService.page(query);

        return ResponseResult.success(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public ResponseResult<SysDictDataVO> get(@PathVariable("id") Long id) {
        SysDictData entity = sysDictDataService.getById(id);
        return ResponseResult.success(SysDictDataConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(businessType = BusinessType.INSERT)
    public ResponseResult<String> save(@RequestBody @Valid SysDictDataVO vo) {
        sysDictDataService.save(vo);

        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(businessType = BusinessType.UPDATE)
    public ResponseResult<String> update(@RequestBody @Valid SysDictDataVO vo) {
        sysDictDataService.update(vo);
        return ResponseResult.success();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(businessType = BusinessType.DELETE)
    public ResponseResult<String> delete(@RequestBody List<Long> idList) {
        sysDictDataService.delete(idList);
        return ResponseResult.success();
    }

}